import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { getWechatId } from './utils';
import registerServiceWorker from './registerServiceWorker';

function sendHttp(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.overrideMimeType("application/json");
    var timer = setTimeout(function () {
        this.timedout = true;
        xhr.abort();
    }, 3000);

    xhr.open("GET", url, false);  // 有风险
    xhr.onreadystatechange = function () {
        if (xhr.readyState !== 4) { return; }
        if (this.timedout) { return; }
        clearTimeout(timer);
        if (xhr.readyState === 4 && xhr.status === 200) {
            callback(xhr.responseText);
        }
    }
    xhr.send(null);
}

var wechat_id = getWechatId();
let url = window.location.protocol + '//' + window.location.host + '/web_api/get_public_platform?wechat_id=' + wechat_id;
sendHttp(url, function (data) {
    try {
        const json = JSON.parse(data);
        if (json.errcode === 0) {
            const result = json.data;
            const config = JSON.parse(result.config);

            localStorage.setItem('album_group_template_id', result.album_group_template_id);
            localStorage.setItem('company_name', result.company_name);
            localStorage.setItem('device_name', result.device_name);
            localStorage.setItem('device_type', result.device_type);
            localStorage.setItem('app_version_code', result.app_version_code || 1);

            // 搜索词
            localStorage.setItem('search_words', config.search_words);
            localStorage.setItem('search_tags', config.search_tags);
            localStorage.setItem('app_logo', config.app_logo);
            // 提示信息列表
            const texts = JSON.stringify(config.text_list);
            localStorage.setItem('text_list', texts);
            // 广告栏
            const banners = JSON.stringify(config.banner);
            localStorage.setItem('banner_list', banners);
            // 类别
            const categorys = JSON.stringify(config.categorys);
            localStorage.setItem('category_list', categorys);
            // 性别组
            const genderList = JSON.stringify(config.genders);
            localStorage.setItem('gender_list', genderList);
            // 头像
            localStorage.setItem('avatar', config.avatar || '');

            let range = '';
            for (let index = 0, len = config.categorys.length; index < len; index++) {
                const item = config.categorys[index];
                if (index < len - 1) {
                    range += item.category_id + ',';
                } else {
                    range += item.category_id;
                }
            }
            localStorage.setItem('category_range', range);
            // 我的界面的收藏类别
            const store_list = JSON.stringify(config.store_list);
            localStorage.setItem('store_list', store_list);
        }
    } catch (error) {
        console.error(error);
    }
})

ReactDOM.render(
    <App />,
    document.getElementById('root'));
registerServiceWorker();

